package com.mall.handler;

import com.mall.exception.*;
import com.mall.vo.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class AllExceptionHandler {

    @ExceptionHandler(Exception.class)
    public WebResult doException(Exception e){
        log.error(e.getMessage());
        return WebResult.builder()
                .code(-999)
                .message("系统异常")
                .build();
    }

    @ExceptionHandler
    public WebResult<String> doInsertException(InsertException e) {
        log.error(e.getMessage());
        return WebResult.<String>builder()
                .code(-998)
                .message(e.getMessage())
                .build();
    }

    @ExceptionHandler
    public WebResult<String> doDeleteException(DeleteException e) {
        log.error(e.getMessage());
        return WebResult.<String>builder()
                .code(-997)
                .message(e.getMessage())
                .build();
    }

    @ExceptionHandler
    public WebResult<String> doNoStockException(NoStockException e) {
        log.error(e.getMessage());
        return WebResult.<String>builder()
                .code(-996)
                .message(e.getMessage())
                .build();
    }

    @ExceptionHandler
    public WebResult<String> doOrderTokenException(OrderTokenException e) {
        log.error(e.getMessage());
        return WebResult.<String>builder()
                .code(-995)
                .message(e.getMessage())
                .build();
    }

    @ExceptionHandler
    public WebResult<String> doPriceMismatchException(PriceMismatchException e) {
        log.error(e.getMessage());
        return WebResult.<String>builder()
                .code(-994)
                .message(e.getMessage())
                .build();
    }
}
